import type { IQuery, IResponse, TableResponse } from '@/api/config/types';
import axios from 'axios';
import { downloadFileTemplate } from '../utils/files';
import type { IResolution } from '../data/resolution';

export interface ITag {
  tagId: string;
  tagCode: string;
  goodsId: string;
  templateId: string;
  mac: null | string;
  resolutionId: string;
  stationId: string;
  tagColor: string;
  bindMode: string;
  bindingTime: string;
  lastUpdateTime: string;
  lastOnlineTime: string;
  status: string;
  rssi: number;
  refreshTimes: number;
  imgSrc: string;
  soc: number;
  centigrade: number;
  power: number;
  rate: number;
  fwVersion: string;
  type: string;
  deptId: string;
  stationName: string;
  stationNum: string;
  goodsName: string;
  templateName: string;
  resolutionDesc: string;
  resolutionWidth: number;
  resolutionHeight: number;
  vlast: number;
  light?: boolean;
  location: string;
  alias: 'a' | 'b' | 'c';
  goodsList: any[];
  store?: string;
}

type Firmware = {
  resolutionId: string;
  version: string;
};

type VersionType = {
  tags: (number | string)[];
  firmware: Firmware;
};

export const listPriceTag = (query?: IQuery) => {
  query = JSON.parse(JSON.stringify(query));
  return axios.get<TableResponse<ITag>>('/priceTag/priceTag/list', { params: query });
};

export const delPriceTag = (tagId: Array<string>) => {
  return axios.delete(`/priceTag/priceTag/${tagId}`);
};

export const updatePriceTag = (data: Partial<ITag>) => {
  return axios.put('/priceTag/priceTag', data);
};

export const addPriceTag = (data: ITag) => {
  return axios.post('/priceTag/priceTag', data);
};

export const upgradeVersion = (data: VersionType) => {
  return axios.post('/firmware/firmware/upgrade', data);
};

export const upgradeFirmware = (data: { tags: Array<string> }) => {
  return axios.post('/firmware/firmware/upgrade', data);
};

export const listFw = (resolutionId: string) => {
  return axios.get<IResponse<string[]>>(`/firmware/firmware/listFw`, { params: { resolutionId } });
};

export const light = (data: { times: number; color: string; tags: (string | number)[] }) => {
  return axios.post<IResponse>('priceTag/priceTag/light', data);
};

export const powerState = () => {
  return axios.get('priceTag/priceTag/powerState');
};

export const downloadTemplate = (url: string) => {
  return axios.get(url);
};

export const downloadFile = () => {
  return downloadFileTemplate('/priceTag/priceTag/export');
};

export const manifestFile = () => {
  return downloadFileTemplate('/priceTag/priceTag/manifest?scope=0');
};

export const transportPart = (to: string, list?: string[]) => {
  return axios.post('/priceTag/priceTag/shift', { list, to });
};

export const transportAll = (to: string, from: string) => {
  return axios.get('/priceTag/priceTag/shift', { params: { to, from } });
};

export const relations = () => {
  return downloadFileTemplate('/priceTag/priceTag/relations?scope=0');
};

export const saveAndSend = (data: Partial<ITag> & { goodsList: any[] }) => {
  return axios.put('/priceTag/priceTag/saveAndSend', data);
};

export const preview = (params: { goodsList: any[]; templateId?: string; tagCode: string }) => {
  return axios.post<IResponse>('/priceTag/priceTag/preview', params);
};
